﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomenModel;
using System.Linq.Expressions;
using Model.Repositories.Interfaces;
using System.Data.Entity;

namespace OmsTestProject.MockRepository
{
    public class OrderRepository : ICrud<Order>
    {
        MockRepositoryContext _db = MockRepositoryContext.MockContextInstance;

        public Order GetById(long Id)
        {
                Order orderById = _db.Orders.FirstOrDefault(p => p.OrderId == Id);
                return orderById;
        }

        public void Save(Order t)
        {
                _db.Orders.Add(t);
        }

        public void Update(Order t)
        {
                _db.Orders[t.OrderId].Assigned = t.Assigned;
                _db.Orders[t.OrderId].OrderDetailses = t.OrderDetailses;
                _db.Orders[t.OrderId].UserId = t.UserId;
                _db.Orders[t.OrderId].user = t.user;
                _db.Orders[t.OrderId].DateOfOrdering = t.DateOfOrdering;
                _db.Orders[t.OrderId].PreferibleDeliveryDate = t.PreferibleDeliveryDate;
                _db.Orders[t.OrderId].DeliveryDate = t.DeliveryDate;
                _db.Orders[t.OrderId].CreditCardId = t.CreditCardId;
                _db.Orders[t.OrderId].CreditCard = t.CreditCard;
                _db.Orders[t.OrderId].TotalPrice = t.TotalPrice;

                _db.Orders[t.OrderId].Accepted = t.Accepted;
                _db.Orders[t.OrderId].Delivered = t.Delivered;
                _db.Orders[t.OrderId].Gift = t.Gift;


        }

        public void Delete(long Id)
        {
                Order orderById = _db.Orders.FirstOrDefault(p => p.OrderId == Id);
                var orderDetailsesThisOrder = _db.OrderDetails.Where(p => p.OrderId == orderById.OrderId);

                foreach (var t in orderDetailsesThisOrder)
                {
                    _db.OrderDetails.Remove(t);
                }

                _db.Orders.Remove(orderById);

        }

        public List<Order> GetCollection()
        {
                return _db.Orders;
        }

        public IQueryable<Order> GetCollectionByFilter(Expression<Func<Order, bool>> predicate)
        {
                var ordersByFilter = _db.Orders.AsQueryable<Order>().Where(predicate);
                return ordersByFilter;
        }
    }
}
